#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <syscalls.h>

#include "gfx.h"

#define MAX_PROCESS 32

int
main(int argc, char **argv)
{
  int i = 0;
  int n_proc;
  int max_height;
  int division_factor = 2;
  struct proc_info proc[32];

  gfx_open();

  max_height = gfx_get_height() - 50;

  while (1)
    {
      /* We ignore the idle process since that will otherwise always
       * be the largest one.
       */

      get_processes(proc, &n_proc);
      for (i = 1 ; i < n_proc; i++)
        if (proc[i].cpu_time / division_factor > max_height)
          division_factor *= 2;

      gfx_clear(BLACK);
      for (i = 1; i < n_proc; i++)
        {
          /* We try to do something smart without using floating point.
           * Kernel won't run for too long, so maximum height is 500 pixels,
           * maximum time we can display 1000 ticks.
           * Each pixel in height stands for 2 ticks.
           */
          int height = proc[i].cpu_time / division_factor;
          if (height > max_height)
            height = max_height;

          gfx_fill_rect(20 + i * 20, 20, 10, height, gfx_color_palette[i-1]);
        }

      sleep(5);
    }

  gfx_close();
}
